﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using FinPlusAssembler;

namespace FinPlusCompView
{
    public class ColHelpers : Dictionary<string, ColHelper>
    {
        public Dictionary<string, string> QuotePopupCols { get; set; }
        public Dictionary<string, string> PodEditorPopupCols { get; set; }
        public IList<string> ListColNames { get; set; }
        public string LinkedId { get; private set; }

        private enum Params { StringFormat, Type, InputMask }
        private IDictionary<string, string[]> _colTypes, _colFormats, _colLinks;
        private RoutedEventHandler _filterbarAdd, _filterbarRemove;
        private FinPlusGroupGrid _grid;
        private string[] _enabledCols;

        //constructor
        public ColHelpers(IDictionary<string, string[]> colTypes, string[] enabledCols, IDictionary<string, string[]> colLinks, IDictionary<string, string[]> colFormats)
        {
            _colTypes = colTypes;
            _enabledCols = enabledCols;
            _colFormats = colFormats;
            _colLinks = colLinks;
            ListColNames = new List<string>();
        }

        //public
        public ColHelper GetMap(string colName, string type)
        {
            ColHelper colMap;

            if (this.ContainsKey(colName))
                return this[colName];

            return colMap = new ColHelper(colName, LinkedId, _filterbarAdd, _filterbarRemove);
        }

        public void Load(FinPlusGroupGrid grid, string linkedId, RoutedEventHandler filterbarAdd, RoutedEventHandler filterbarRemove)
        {
            _grid = grid;
            LinkedId = linkedId;
            _filterbarAdd = filterbarAdd;
            _filterbarRemove = filterbarRemove;
            ColTypes();
            ColEnable();
            ColLinks();
            ColFormats();
        }

        public bool HasCol(string name)
        {
            if (ListColNames.Contains(name))
                return true;

            if (QuotePopupCols != null && (QuotePopupCols.ContainsValue(name) || QuotePopupCols.ContainsKey(name)))
                return true;

            if (PodEditorPopupCols != null && (PodEditorPopupCols.ContainsValue(name) || PodEditorPopupCols.ContainsKey(name)))
                return true;

            return false;
        }

        //private
        private void ColTypes()
        {
            try
            {
                if (_colTypes == null) return;

                foreach (var colType in _colTypes)
                    foreach (var colName in colType.Value)
                        this[colName] = new ColHelper(colName, LinkedId, _filterbarAdd, _filterbarRemove, colType.Key);
            }
            catch (Exception e)
            {
                Level.Error.Log("NA", "NA", e.Message, e);
            }
        }

        private void ColEnable()
        {
            try
            {
                if (_enabledCols == null) 
                    return;

                foreach (var colName in _enabledCols)
                {
                    if (!this.ContainsKey(colName))
                        this[colName] = new ColHelper(colName, LinkedId, _filterbarAdd, _filterbarRemove);
                    this[colName].IsEnabled = true;
                }
            }
            catch (Exception e)
            {
                Level.Error.Log("NA", "NA", e.Message, e);
            }
        }

        private void ColLinks()
        {
            try
            {
                if (_colLinks == null) 
                    return;

                foreach (var colLink in _colLinks)
                {
                    foreach (var colName in colLink.Value)
                    {
                        if (!this.ContainsKey(colName))
                            this[colName] = new ColHelper(colName, LinkedId, _filterbarAdd, _filterbarRemove);
                        var colMap = this[colName];
                        colMap.Links = colMap.Value = colLink.Key;
                    }
                }
            }
            catch (Exception e)
            {
                Level.Error.Log("NA", "NA", e.Message, e);
            }
        }

        private void ColFormats()
        {
            try
            {
                if (_colFormats == null) 
                    return;

                foreach (var colFormat in _colFormats)
                {
                    foreach (var colName in colFormat.Value)
                    {
                        if (!this.ContainsKey(colName))
                            this[colName] = new ColHelper(colName, LinkedId, _filterbarAdd, _filterbarRemove);
                        var colMap = this[colName];
                        var format = (Hashtable)_grid.FindResource(colFormat.Key);
                        colMap.StringFormat = format[Params.StringFormat.ToString()].ToString();
                        colMap.Type = format[Params.Type.ToString()].ToString();
                        colMap.InputMask = format[Params.InputMask.ToString()].ToString();
                    }
                }
            }
            catch (Exception e)
            {
                Level.Error.Log("NA", "NA", e.Message, e);
            }
        }
    }
}
